import FileSaver from 'file-saver'
function downloadBlob(buf,fileName) {
	let ab = Buffer.from(buf, "binary");
	
		// const blob = new Blob([ab], {
		// 	type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'
		// });
		// var array = new Int8Array([17, -45.3]);
		
	try{
	  var blob = new Blob( [ab], {type : "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8"});
	}
	catch(e){
	    // TypeError old chrome and FF
	    window.BlobBuilder = window.BlobBuilder ||
	                         window.WebKitBlobBuilder ||
	                         window.MozBlobBuilder ||
	                         window.MSBlobBuilder;
	    if(e.name == 'TypeError' && window.BlobBuilder){
	        var bb = new BlobBuilder();
	        bb.append([ab]);
	        var blob = bb.getBlob("application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8");
	    }
	    else if(e.name == "InvalidStateError"){
	        // InvalidStateError (tested on FF13 WinXP)
	        var blob = new Blob( [ab], {type : "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8"});
	    }
	    else{
	        // We're screwed, blob constructor unsupported entirely
	    }
	}
	
	
	const filename = fileName
	if (window.navigator.msSaveOrOpenBlob) {
		navigator.msSaveBlob(blob, filename);
	} else {
		const link = document.createElement("a");
		const body = document.querySelector("body");

		link.href = window.URL.createObjectURL(blob); // 创建对象url
		console.log(link.href)
		link.download = filename;

		// fix Firefox
		link.style.display = "none";
		body.appendChild(link);

		link.click();
		body.removeChild(link);

		window.URL.revokeObjectURL(link.href); // 通过调用 URL.createObjectURL() 创建的 URL 对象
	}
	
}
export default downloadBlob;
